package com.xxgc.qinglian.qinglianserver.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.xxgc.qinglian.qinglianserver.entity.VoteEntity;
import com.xxgc.qinglian.qinglianserver.result.Result;
import com.xxgc.qinglian.qinglianserver.service.VoteService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("vote")
@RequiredArgsConstructor
public class VoteController {


    private final VoteService voteService;

    @GetMapping("/getAllVote")
    public Result getAllVotes() {
        //当前用户
        String loginId = (String) StpUtil.getLoginId();

        //当前用户创建的
        List<VoteEntity> votes = Db.lambdaQuery(VoteEntity.class).eq(VoteEntity::getUserId, loginId).list();
        //大一大二大三固定都有的

        votes.addAll(Db.lambdaQuery(VoteEntity.class).eq(VoteEntity::getUserId, "3").list());


        return Result.success(votes).msg("获取所有项目数据成功");
    }

    @PostMapping("insertVote")
    public Result insertVote(@RequestBody VoteEntity voteEntity) {
        String loginId = (String) StpUtil.getLoginId();
        voteEntity.setUserId(loginId);
        voteEntity.setCreateTime(LocalDateTime.now());
        voteService.insert(voteEntity);
        return Result.success();
    }

    @PutMapping("updateVote")
    public Result updateVote(@RequestBody VoteEntity voteEntity) {
        voteService.update(voteEntity);
        return Result.success();
    }


}
